#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h" // 必须以这个顺序 include，由于三个头文件有依赖关系

int main(int argc,char* argv[])
{
    int p1[2];
    int p2[2];
    if(pipe(p1) < 0 || pipe(p2) < 0)
    {
        printf("pipe failed\n");
        exit(1);
    }
    char buf;
    int pid = fork();
    if(pid == 0)
    {
        //第一个管道使用读，第二个管道使用写
        close(p1[1]);
        close(p2[0]);
        if(read(p1[0],&buf,1) < 0)
        {
            printf("read error\n");
            exit(1);
        }
        printf("<%d>: received ping\n",getpid());
        if(write(p2[1],&buf,1) < 0)
            printf("child write error\n");
    }
    else
    {
        close(p1[0]);
        close(p2[1]);
        if(write(p1[1],&buf,1) < 0)
        {
            printf("write error\n");
            exit(1);
        }
        if(read(p2[0],&buf,1) < 0)
        {
            printf("father read error\n");
            exit(1);
        }
        printf("<%d>: received pong\n",getpid());
        wait(0);
    }
    exit(0);
}